package com.mactivity.activities;

import java.util.ArrayList;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

import com.mactivity.database.HomeActivityFunctions;
import com.mactivity.helper.Constants;
import com.mactivity.helper.SharedPreferenceHelper;
import com.mactivity.helper.TimeConverterHelper;

/**
 * Author Sanjay Vasudeva
 * Class HomeActivity - Handles bringing up Dashboard and actions on clicking of icons
 * **/

public class HomeActivity extends Activity {
	SharedPreferenceHelper preferenceHelper;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork() // or	.detectAll() for all detectable problems
		.penaltyLog().build());

        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);
        //getting the User ID of the current user logged in using shared preference.
        preferenceHelper =  new SharedPreferenceHelper(this);
        Map<String, String> userInfo=preferenceHelper.getUserInformation();
        //I has the User ID
 		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
 		Integer I=new Integer(Integer.parseInt(userInfo.get(Constants.USER_ID)));
 		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_USERID,I.toString()));
 		String response = null;
 		//Array to show the day next to date in dashboard
 		String week[]={"Su","Mo","Tu","We","Th","Fr","Sa"};
 		try{
 			//Database calls from HomeActivityFunctions
 			HomeActivityFunctions homeActivityFunctions=new HomeActivityFunctions();
 			//Clearing the data previous to current date
 			homeActivityFunctions.cleanDatabase(I);
 			//Call getAcceptedGroupPlans to get the Group Plans which the user has accepted to join
 			response=homeActivityFunctions.getAcceptedGroupPlans(I);
 			//Dashboard table
	        TableLayout table = (TableLayout) findViewById(R.id.DashboardGroupTable);
	        String dashbSport="";String dashbDate="";String dashbTime="";
	        //if condition to check if the response does not have null
	        if(response.length()>5){
	        	JSONArray jArray = new JSONArray(response);
	        	for(int i=0;i<jArray.length();i++){
	        		//getting the JSON objects one at a time from the response
	        		JSONObject json_data = jArray.getJSONObject(i);
	        		dashbSport=json_data.getString(Constants.JSON_STRING_SPORT);
	        		dashbDate=json_data.getString(Constants.JSON_STRING_DATE);
	        		dashbTime=json_data.getString(Constants.JSON_STRING_TIME);
    				int dashbDay=json_data.getInt(Constants.JSON_STRING_DAY);
    				//day and month extraction
    				String dashBDate=dashbDate.substring(5);
    				String day=week[dashbDay-1];
    				//concatinating the day
    				String dashDate=new String(dashBDate+"("+day+")");
	        		TableRow row = new TableRow(this);
	        		TextView Type=new TextView(this) ;
	        		TextView Sport=new TextView(this) ;
	        		TextView Date=new TextView(this) ;
	        		TextView Time=new TextView(this) ;
	        		Type.setWidth(50);
	        		//GPothr means Group Plan from others
	        		Type.setText("GPOthr");
	        		//Set the column values.
	        		Sport.setWidth(100);
	        		Sport.setText(dashbSport);
	        		Date.setWidth(100);
	        		Date.setText(dashDate);
	        		Time.setWidth(80);
	        		TimeConverterHelper timeConverterHelper=new TimeConverterHelper();
	        		String dashboardTime=timeConverterHelper.ConvertTimeToAmPm(dashbTime);
	        		Time.setText(dashboardTime);
	        		row.addView(Type); 
	        		row.addView(Sport);
	        		row.addView(Date);
	        		row.addView(Time);
	        		table.addView(row);
	        		//Draw line.
	        		View line = new View(this);
	        		line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
	        		line.setBackgroundColor(Color.rgb(51, 51, 51));
	        		table.addView(line);
	        	}
	        }
	        response =homeActivityFunctions.getMyGroupPlans(I);
    		if(response.length()>5){
    			JSONArray jArray1 = new JSONArray(response);
    			for(int i=0;i<jArray1.length();i++){
    				JSONObject json_data = jArray1.getJSONObject(i);
    				dashbSport=json_data.getString(Constants.JSON_STRING_SPORT);
    				dashbDate=json_data.getString(Constants.JSON_STRING_DATE);
    				dashbTime=json_data.getString(Constants.JSON_STRING_TIME);
    				int dashbDay=json_data.getInt(Constants.JSON_STRING_DAY);
    				String dashBDate=dashbDate.substring(5);
    				String day=week[dashbDay-1];
    				String dashDate=new String(dashBDate+"("+day+")");
    				TableRow row = new TableRow(this);
    				TextView Type=new TextView(this) ;
    				TextView Sport=new TextView(this) ;
    				TextView Date=new TextView(this) ;
    				TextView Time=new TextView(this) ;
    				Type.setWidth(50);
	        		//MyGP means Group Plan made by user
    				Type.setText("MyGP");
    				Sport.setWidth(100);
    				Sport.setText(dashbSport);
    				Date.setWidth(100);
    				Date.setText(dashDate);
    				Time.setWidth(80);
    				TimeConverterHelper timeConverterHelper=new TimeConverterHelper();
	        		String dashboardTime=timeConverterHelper.ConvertTimeToAmPm(dashbTime);
	        		Time.setText(dashboardTime);
    				row.addView(Type); 
    				row.addView(Sport);
    				row.addView(Date);
    				row.addView(Time);
    				table.addView(row);
    				View line = new View(this);
    				line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
    				line.setBackgroundColor(Color.rgb(51, 51, 51));
    				table.addView(line);
    			}
    		}
	        response =homeActivityFunctions.getMyIndividualPlans(I);
    		if(response.length()>5){
    			JSONArray jArray1 = new JSONArray(response);
    			for(int i=0;i<jArray1.length();i++){
    				JSONObject json_data = jArray1.getJSONObject(i);
    				dashbSport=json_data.getString(Constants.JSON_STRING_SPORT);
    				dashbDate=json_data.getString(Constants.JSON_STRING_DATE);
    				dashbTime=json_data.getString(Constants.JSON_STRING_TIME);
    				int dashbDay=json_data.getInt(Constants.JSON_STRING_DAY);
    				String dashBDate=dashbDate.substring(5);
    				String day=week[dashbDay-1];
    				String dashDate=new String(dashBDate+"("+day+")");
    				TableRow row = new TableRow(this);
    				TextView Type=new TextView(this) ;
    				TextView Sport=new TextView(this) ;
    				TextView Date=new TextView(this) ;
    				TextView Time=new TextView(this) ;
    				Type.setWidth(50);
    				//I- Individual Plan
    				Type.setText("I");
    				Sport.setWidth(100);
    				Sport.setText(dashbSport);
    				Date.setWidth(100);
    				Date.setText(dashDate);
    				Time.setWidth(80);
    				TimeConverterHelper timeConverterHelper=new TimeConverterHelper();
	        		String dashboardTime=timeConverterHelper.ConvertTimeToAmPm(dashbTime);
	        		Time.setText(dashboardTime);
    				row.addView(Type); 
    				row.addView(Sport);
    				row.addView(Date);
    				row.addView(Time);
    				table.addView(row);
    				View line = new View(this);
    				line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
    				line.setBackgroundColor(Color.rgb(51, 51, 51));
    				table.addView(line);
    			}
    		}
  		}
 		catch(Exception e){
			Log.e("log_tag","Error in http connection!!" + e.toString());					
		}
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbar_menu, menu);
        return true;
    }
      
        
    public void click(MenuItem item){
    	Intent myIntent = new Intent();
    	switch(item.getItemId()){
    	case R.id.menu_plan:  	
    		myIntent = new Intent(HomeActivity.this, PlanEventActivity.class);
    		startActivity(myIntent);
    		//closeOptionsMenu();
    		finish();
    		break;
    	case R.id.menu_notification:
    		myIntent = new Intent(HomeActivity.this, ProfileTabActivity.class);
    		startActivity(myIntent);
    		finish();
    		break;
    	case R.id.menu_macboard:
    		myIntent = new Intent(HomeActivity.this, NotificationActivity.class);
    		startActivity(myIntent);
    		finish();
    		break;
    	case R.id.menu_friends:
    		myIntent = new Intent(HomeActivity.this, FriendListTabActivity.class);
    		startActivity(myIntent);
    		finish();
    		break;
    	case R.id.menu_chart:
    		myIntent = new Intent(HomeActivity.this, ChartsActivity.class);
    		startActivity(myIntent);
    		finish();
    		break;
    	default: ;//do nothing
    	}
    }
}